#-*- coding: utf-8 -*-
#funcs.py
import pygame, os
images = dict()
animimg = dict()

def img(name):
    global images
    try:
        return images[name]
    except:
        try:
            images[name] = pygame.image.load(name).convert_alpha()
            return images[name]
        except:
            print "img() in funcs.py: Problem with image\n"
            return pygame.Surface((128,128))
            
def load_anim_img(name):
    global animimg
    try:
        return animimg[name]
    except:
        try:
            direct = os.listdir(name)
            direct.sort()
            animimg[name] = list()
            
            for x in direct:
                if x != ".svn":
                    animimg[name].append(pygame.image.load(name+"/"+x).convert_alpha())
            
            return animimg[name]
        except:
            print "load_anim_img() in funcs.py: Problem with image\n"

def printsur(size, text, color, dire, pos, dest):
    textsplit = list()
    last=0
    for x in range(len(text)):
        if text[x] == "~" and text[x+1] == "n":
            last=x
            if len(textsplit) == 0:
                textsplit.append(text[:x])
            for y in range(x+2, len(text)):
                if text[y] == "~" and text[y+1] == "n":
                    textsplit.append(text[x+2:y])
                    break;
    if not last:
        textsplit.append(text[:])
    else:
        textsplit.append(text[last+2:])
            
    for x in range(len(textsplit)):        
        font = pygame.font.Font("data/arial.ttf", size)
        textt = font.render(textsplit[x], 1, color)
        if len(textsplit) <= 1:
            textpos = eval("textt.get_rect(%s=pos)" % dire)
        else:
            textpos = eval("textt.get_rect(%s=(pos[0], pos[1]+x*(size+5)))" % dire)
        dest.blit(textt, textpos)
        
def drawrects(x, y, w, h, rgba, dest):
    rec = pygame.Surface([abs(w), abs(h)])
    rec.fill((rgba[0],rgba[1],rgba[2]))
    alpha = rgba[3]*255/100 #0-100% alpha
    rec.set_alpha(alpha)
    if w < 0:
        x += w
    if h < 0:
        y += h
    dest.blit(rec, (x, y))
    
    return pygame.Rect(x,y,abs(w),abs(h))

def makeSurface(w, h, rgba):
    rec = pygame.Surface([abs(w), abs(h)])
    rec.fill((rgba[0],rgba[1],rgba[2]))
    alpha = rgba[3]*255/100 #0-100% alpha
    rec.set_alpha(alpha)
    if w < 0:
        x += w
    if h < 0:
        y += h
    
    return rec

    
def ColRects(rect1, rect2):
    if rect1.top > rect2.top and rect1.top < rect2.bottom and rect1.left < rect2.left and rect1.right > rect2.right:
        return True
    if rect1.bottom > rect2.top and rect1.bottom < rect2.bottom and rect1.left < rect2.left and rect1.right > rect2.right:
        return True
    if rect2.top > rect1.top and rect2.top < rect1.bottom and rect2.left < rect1.left and rect2.right > rect1.right:
        return True
    if rect2.bottom > rect1.top and rect2.bottom < rect1.bottom and rect2.left < rect1.left and rect2.right > rect1.right:
        return True
    if rect1.top > rect2.top and rect1.top < rect2.bottom and rect1.left > rect2.left and rect1.right < rect2.right:
        return True
    if rect1.bottom > rect2.top and rect1.bottom < rect2.bottom and rect1.left > rect2.left and rect1.right < rect2.right:
        return True
    if rect1.top < rect2.top and rect1.bottom > rect2.bottom and rect1.left < rect2.left and rect1.right > rect2.right:
        return True
    if rect1.top < rect2.bottom and rect1.bottom > rect2.top and rect1.left > rect2.left and rect1.left < rect2.right:
        return True
    if rect1.top < rect2.bottom and rect1.bottom > rect2.top and rect1.right > rect2.left and rect1.right < rect2.right:
        return True
    return False #just in case
        
    

              
